package resolve;

import cn.org.rapid_framework.generator.util.GLogger;
import cn.org.rapid_framework.generator.util.PropertiesHelper;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import static cn.org.rapid_framework.generator.GeneratorProperties.setProperties;

public class GeneratorProperties {

    static PropertiesHelper propertiesHelper = new PropertiesHelper(new Properties(), true);

    public static void setProperties(Properties inputProps) {
        propertiesHelper = new PropertiesHelper(inputProps, true);
        Iterator it = propertiesHelper.entrySet().iterator();

        while(it.hasNext()) {
            Map.Entry entry = (Map.Entry)it.next();
            GLogger.debug("[Property] " + entry.getKey() + "=" + entry.getValue());
        }

        GLogger.println("");
    }

    private static void setSepicalProperties(Properties p, String[] loadedFiles) {
        if (loadedFiles != null && loadedFiles.length > 0) {
            String basedir = p.getProperty("basedir");
            if (basedir != null && basedir.startsWith(".")) {
                p.setProperty("basedir", (new File((new File(loadedFiles[0])).getParent(), basedir)).getAbsolutePath());
            }
        }

    }
    public static void reload() {
        try {
            //GLogger.println("Start Load GeneratorPropeties from classpath:" + Arrays.toString(PROPERTIES_FILE_NAMES));
            Properties p = new Properties();
            String[] loadedFiles = PropertiesHelper.loadAllPropertiesFromClassLoader(p, "");
            //GLogger.println("GeneratorPropeties Load Success,files:" + Arrays.toString(loadedFiles));
            setSepicalProperties(p, loadedFiles);
            setProperties(p);
        } catch (IOException var2) {
            throw new RuntimeException("Load " + "" + " error", var2);
        }
    }
}
